package zhtt.jasperreports02.controller;

import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.mysql.jdbc.JDBC4ResultSet;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.JRTextExporter;
import net.sf.jasperreports.engine.export.JRTextExporterParameter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import zhtt.jasperreports02.db.MysqlUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class MysqlController {

    @RequestMapping(value = "demo02/report02", method = RequestMethod.GET)
    public String report02(Model model) throws SQLException {
        Connection connection = MysqlUtils.getConnection();
        String sql = "select * from zhtt_user";
        JDBC4ResultSet  rs = (JDBC4ResultSet) connection.createStatement().executeQuery(sql);
        // 报表数据源
        JRDataSource jrDataSource = new JRResultSetDataSource(rs);
        // 动态指定报表模板url
        model.addAttribute("url", "/WEB-INF/classes/reports/report2.jasper");
        model.addAttribute("format", "pdf"); // 报表格式
        model.addAttribute("jrMainDataSource", jrDataSource);
        return "iReportView"; // 对应jasper-defs.xml中的bean id
    }

    @RequestMapping(value = "demo02/report05", method = RequestMethod.GET)
    public String report05(Model model, HttpServletRequest request, HttpServletResponse resp) throws SQLException, IOException {
        resp.setContentType("application/pdf");
        ServletOutputStream output= resp.getOutputStream();
        InputStream reportStream = request.getServletContext().getResourceAsStream("/WEB-INF/classes/reports/report5.jasper");
        Connection connection = MysqlUtils.getConnection();
        HashMap parameterMap = new HashMap();
        parameterMap.put("title", new String("%1"));
        try{
            JasperRunManager.runReportToPdfStream(reportStream,output,parameterMap,connection);
            //JasperExportManager.exportReportToHtmlFile();
            connection.close();
            output.flush();
            output.close();
        }catch(Exception e){
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            resp.setContentType("text/plain");
            resp.getOutputStream().print(stringWriter.toString());
        }
        return "iReportView";
    }


    public static void export() throws Exception{
        String file = "F:\\workspace-intellij\\spring4\\demob1_jasperreports\\src\\main\\resources\\reports\\report2.jasper";
        InputStream inputStream = MysqlController.class.getResourceAsStream(file);
        Map parameters = new HashMap();
        Connection connection = MysqlUtils.getConnection();
        JasperPrint jasperPrint = JasperFillManager.fillReport(file,parameters,connection);
        JRTextExporter exporter = new JRTextExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        /*文件会被输出到项目根目录下*/
        exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "db.txt");
        exporter.setParameter(JRTextExporterParameter.PAGE_WIDTH, 200);
        exporter.setParameter(JRTextExporterParameter.PAGE_HEIGHT, 100);
        exporter.exportReport();
    }

    public static void main(String[] args) throws Exception{
        export();
    }
}
